using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class asp_pages_PetManager : System.Web.UI.Page
{
    PetCategoryDAO daoPetCate = new PetCategoryDAO();
    PetDAO daoPet = new PetDAO();
    public void LoadInitData()
    {
        
        if (!IsPostBack)
        {
            cmbPetCategories.DataSource = daoPetCate.GetAllCategories();
            cmbPetCategories.DataTextField = "Name";
            cmbPetCategories.DataValueField = "ID";
            cmbPetCategories.DataBind();
            txtName.Text = "";
            txtHeight.Text = "0";
            txtWeight.Text = "0";
            txtAvailableNumber.Text = "0";
            txtPrice.Text = "0";
            fckEditor.Value = "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadInitData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblPreviewContent.Text = fckEditor.Value;
    }
    protected void lnkEditInfor_Click(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;
        
        string path = ConfigurationManager.AppSettings["image_path"];
        Pet pet = new Pet();
        pet.Name = txtName.Text;
        pet.Height = float.Parse(txtHeight.Text);
        pet.Weight = float.Parse(txtWeight.Text);
        pet.Category.ID = int.Parse(cmbPetCategories.SelectedValue);
        pet.ImageURL =path+ fupImageURL.FileName;        
        pet.Price = decimal.Parse(txtPrice.Text);
        pet.Quantity = int.Parse(txtAvailableNumber.Text);
        pet.TypeID = 1;//is pet
        pet.Information = fckEditor.Value;
        pet.ModifiedDate = DateTime.Now;
        pet.SaleOff = 0;
        
        if (daoPet.InsertPet(pet) > 0)
        {
            //save image file to server
            fupImageURL.SaveAs(Server.MapPath(path) + fupImageURL.FileName);
            lblMessage.Text = "New pet has been added successfully";
            txtName.Text = "";
            txtHeight.Text = "0";
            txtWeight.Text = "0";
            txtAvailableNumber.Text = "0";
            txtPrice.Text = "0";
            fckEditor.Value = "";
        }
        else
        {
            lblMessage.Text = "Sorry. Error has occured and the information is not added to database.";
        }
    }
    protected void btnPrevInPage_Click(object sender, EventArgs e)
    {
       
    }
}
